#include "vec.h"

// Creates a new vector with N elements of size _size
vec* createVector(unsigned elements, unsigned _size) {
	vec* vector;
	vector = (vec *)malloc(sizeof(vec));

	vector->elements = elements;
	vector->size = _size;	
	elements = (elements == 0 ? 1 : elements);
	vector->capacity = elements * 2;

	void* array = malloc(sizeof(void *) * vector->capacity);
	vector->array = array;
	
	return vector;
}

// Adds a new element to the vector
int addElement(vec *vector, void *element) {

	if (vector->elements >= vector->capacity) 
		vector->array = realloc(vector->array, vector->capacity * 2 * vector->size);

	vector->array[vector->elements] = element;
	vector->elements += 1;

	return 0;
}

// Removes an element, dirty style
int removeElement(vec *vector, int idx) {
	
	if (idx < 0 || idx > vector->elements) {
		#if DEBUG
			puts("Error removing elements (element out of range)");
		#endif		
		return -1;
	}

	for (int i = idx; i < vector->elements - 1; i++) {
		vector->array[i] = vector->array[i + 1];
	}
	
	vector->elements -= 1;
	// TODO remove unused memory
}

